﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_F4CheckRR732 : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private DataSet ds;
        private DataViewManager dsView;

        private Dictionary<string, string> required_list;

        public frm_Add_F4CheckRR732()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ds = new DataSet("RobotOrderDetail");

            this.required_list = new Dictionary<string, string>() { { "serialNo", "Serial No" }, { "writtenBy", "Written By" },
            { "checkBy", "Check By"}, {"inputDate", "Date"}, {"wiringDiagram", "Wiring Diagram"}, {"PC_Power_Serial", "PC Power Serial"},
            {"Upper_RC420", "Upper RC420"}, {"Upper_RC420Ver", "Upper RC420 Ver"}, {"Upper_RC420No", "Upper RC420 No"},
            {"Upper_RD026", "Upper RD026"}, {"Upper_Run", "Upper Run"}, {"Upper_Stop", "Upper Stop"},
            {"Lower_RC420", "Lower RC420"}, {"Lower_RC420Ver", "Lower RC420 Ver"}, {"Lower_RC420No", "Lower RC420 No"},
            {"Lower_RD026", "Lower RD026"}, {"Lower_Run", "Lower Run"}, {"Lower_Stop", "Lower Stop"},
            {"A_RC420", "A RC420"}, {"A_RC420Ver", "A RC420 Ver"}, {"A_RC420No", "A RC420 No"},
            {"A_RD026", "A RD026"}, {"A_Run", "A Run"}, {"A_Stop", "A Stop"},
            {"Bar_RC420", "Bar RC420"}, {"Bar_RC420Ver", "Bar RC420 Ver"}, {"Bar_RC420No", "Bar RC420 No"},
            {"Bar_RD026", "Bar RD026"}, {"Bar_Run", "Bar Run"}, {"Bar_Stop", "Bar Stop"},
            {"Z_RC420", "Z RC420"}, {"Z_RC420Ver", "Z RC420 Ver"}, {"Z_RC420No", "Z RC420 No"},
            {"Z_RD026", "Z RD026"}, {"Z_Run", "Z Run"}, {"Z_Stop", "Z Stop"},
            {"RC431", "RC431"}, {"RC431Ver", "RC431 Ver"}, {"RC431No", "RC431 No"}};

            LoadSerialList();
            LoadStaffList();
            LoadDefaultData();            
        }

        ////////////////////////////////////////////////////
        /// Load Serial List
        ////////////////////////////////////////////////////
        private void LoadSerialList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo], [CompID] FROM [231_RobotOrderDetail]"
                    + " WHERE(FinishedDate > DateAdd(dd, 0, DateDiff(dd, 0, GETDATE())) - 500) AND [SerialNo] NOT IN"
                    + " (SELECT [SerialNo] COLLATE Vietnamese_CI_AS FROM [231_UnitRR732])";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "RobotOrderDetail");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.serialNo.DataSource = dsView;
                this.serialNo.DisplayMember = "RobotOrderDetail.SerialNo";
                this.serialNo.ValueMember = "RobotOrderDetail.SerialNo";
                this.serialNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partNo.DataBindings.Add("Text", dsView, "RobotOrderDetail.CompID");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Load Staff List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadStaffList()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT StaffID, StaffName FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffID";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    string[] fullname = dataReader["StaffName"].ToString().Trim().Split(' ');

                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    string txt = textInfo.ToTitleCase(fullname[fullname.Length - 1].ToLower());

                    item.Text = txt + dataReader["StaffID"].ToString().Trim();
                    //item.Text = dataReader["StaffID"].ToString().Trim() + " - " + dataReader["StaffName"].ToString().Trim();
                    item.Value = dataReader["StaffID"].ToString().Trim();

                    this.writtenBy.Properties.Items.Add(item);
                    this.checkBy.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        ////////////////////////////////////////////////////
        /// Load Default Data
        ////////////////////////////////////////////////////
        private void LoadDefaultData()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Wiring Daiagram]"
                    + " FROM [231_UnitRR732]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Wiring Daiagram"].ToString().Trim();
                    item.Text = dataReader["Wiring Daiagram"].ToString().Trim();

                    this.wiringDiagram.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [PC software ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["PC software ver"].ToString().Trim();
                    item.Text = dataReader["PC software ver"].ToString().Trim();

                    this.PC_Software_Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [PC IP]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["PC IP"].ToString().Trim();
                    item.Text = dataReader["PC IP"].ToString().Trim();

                    this.PC_IP.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Host name]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Host name"].ToString().Trim();
                    item.Text = dataReader["Host name"].ToString().Trim();

                    this.Host_Name.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [RC400 ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["RC400 ver"].ToString().Trim();
                    item.Text = dataReader["RC400 ver"].ToString().Trim();

                    this.RC400_Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [RS485 ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["RS485 ver"].ToString().Trim();
                    item.Text = dataReader["RS485 ver"].ToString().Trim();

                    this.RS485_Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [PC Power]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["PC Power"].ToString().Trim();
                    item.Text = dataReader["PC Power"].ToString().Trim();

                    this.PC_Power.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //Upper
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Upper RC420 Ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Upper RC420 Ver"].ToString().Trim();
                    item.Text = dataReader["Upper RC420 Ver"].ToString().Trim();

                    this.Upper_RC420Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Upper RC420 Ex ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Upper RC420 Ex ver"].ToString().Trim();
                    item.Text = dataReader["Upper RC420 Ex ver"].ToString().Trim();

                    this.Upper_RC420ExVer.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Upper RC420 No]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Upper RC420 No"].ToString().Trim();
                    item.Text = dataReader["Upper RC420 No"].ToString().Trim();

                    this.Upper_RC420No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Upper run]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Upper run"].ToString().Trim();
                    item.Text = dataReader["Upper run"].ToString().Trim();

                    this.Upper_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Upper stop]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Upper stop"].ToString().Trim();
                    item.Text = dataReader["Upper stop"].ToString().Trim();

                    this.Upper_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //A2
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Lower RC420 Ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Lower RC420 Ver"].ToString().Trim();
                    item.Text = dataReader["Lower RC420 Ver"].ToString().Trim();

                    this.Lower_RC420Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Lower RC420 Ex ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Lower RC420 Ex ver"].ToString().Trim();
                    item.Text = dataReader["Lower RC420 Ex ver"].ToString().Trim();

                    this.Lower_RC420ExVer.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Lower RC420 No]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Lower RC420 No"].ToString().Trim();
                    item.Text = dataReader["Lower RC420 No"].ToString().Trim();

                    this.Lower_RC420No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Lower run]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Lower run"].ToString().Trim();
                    item.Text = dataReader["Lower run"].ToString().Trim();

                    this.Lower_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Lower stop]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Lower stop"].ToString().Trim();
                    item.Text = dataReader["Lower stop"].ToString().Trim();

                    this.Lower_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //A
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A RC420 Ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A RC420 Ver"].ToString().Trim();
                    item.Text = dataReader["A RC420 Ver"].ToString().Trim();

                    this.A_RC420Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A RC420 Ex ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A RC420 Ex ver"].ToString().Trim();
                    item.Text = dataReader["A RC420 Ex ver"].ToString().Trim();

                    this.A_RC420ExVer.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A RC420 No]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A RC420 No"].ToString().Trim();
                    item.Text = dataReader["A RC420 No"].ToString().Trim();

                    this.A_RC420No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A run]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A run"].ToString().Trim();
                    item.Text = dataReader["A run"].ToString().Trim();

                    this.A_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A stop]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A stop"].ToString().Trim();
                    item.Text = dataReader["A stop"].ToString().Trim();

                    this.A_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //Bar                
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Bar RC420 Ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Bar RC420 Ver"].ToString().Trim();
                    item.Text = dataReader["Bar RC420 Ver"].ToString().Trim();

                    this.Bar_RC420Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Bar RC420 Ex ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Bar RC420 Ex ver"].ToString().Trim();
                    item.Text = dataReader["Bar RC420 Ex ver"].ToString().Trim();

                    this.Bar_RC420ExVer.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Bar RC420 No]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Bar RC420 No"].ToString().Trim();
                    item.Text = dataReader["Bar RC420 No"].ToString().Trim();

                    this.Bar_RC420No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Bar run]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Bar run"].ToString().Trim();
                    item.Text = dataReader["Bar run"].ToString().Trim();

                    this.Bar_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Bar stop]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Bar stop"].ToString().Trim();
                    item.Text = dataReader["Bar stop"].ToString().Trim();

                    this.Bar_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //Z
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z RC420 Ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z RC420 Ver"].ToString().Trim();
                    item.Text = dataReader["Z RC420 Ver"].ToString().Trim();

                    this.Z_RC420Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z RC420 Ex ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z RC420 Ex ver"].ToString().Trim();
                    item.Text = dataReader["Z RC420 Ex ver"].ToString().Trim();

                    this.Z_RC420ExVer.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z RC420 No]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z RC420 No"].ToString().Trim();
                    item.Text = dataReader["Z RC420 No"].ToString().Trim();

                    this.Z_RC420No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z run]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z run"].ToString().Trim();
                    item.Text = dataReader["Z run"].ToString().Trim();

                    this.Z_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z stop]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z stop"].ToString().Trim();
                    item.Text = dataReader["Z stop"].ToString().Trim();

                    this.Z_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //RC431
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [RC431 ROM ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["RC431 ROM ver"].ToString().Trim();
                    item.Text = dataReader["RC431 ROM ver"].ToString().Trim();

                    this.RC431_ROM_Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [RC431 ver]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["RC431 ver"].ToString().Trim();
                    item.Text = dataReader["RC431 ver"].ToString().Trim();

                    this.RC431Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [RC431 no]"
                    + " FROM [231_UnitRR732]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["RC431 no"].ToString().Trim();
                    item.Text = dataReader["RC431 no"].ToString().Trim();

                    this.RC431No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo] FROM [231_F5Detail]"
                    + " WHERE ([FinishDate] > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) - 500)";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    item.Text = dataReader["SerialNo"].ToString().Trim();

                    this.PC_Power_Serial.Properties.Items.Add(item);
                    this.Upper_RC420.Properties.Items.Add(item);
                    this.Upper_RD026.Properties.Items.Add(item);
                    this.Lower_RC420.Properties.Items.Add(item);                    
                    this.Lower_RD026.Properties.Items.Add(item);
                    this.A_RC420.Properties.Items.Add(item);                    
                    this.A_RD026.Properties.Items.Add(item);
                    this.Bar_RC420.Properties.Items.Add(item);
                    this.Bar_RD026.Properties.Items.Add(item);
                    this.Z_RC420.Properties.Items.Add(item);
                    this.Z_RD026.Properties.Items.Add(item);
                    this.RC431.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            if (this.serialNo.SelectedIndex < 0)
            {
                error = "Please select Serial No.";
            }

            if (string.IsNullOrEmpty(error))
            {
                foreach (Control c in this.Controls)
                {
                    if (c is System.Windows.Forms.ComboBox)
                    {
                        if ((((System.Windows.Forms.ComboBox)c).SelectedIndex < 0) && string.IsNullOrEmpty(((System.Windows.Forms.ComboBox)c).Text))
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.ComboBox)c).Name))
                            {
                                error = "Please select " + required_list[((System.Windows.Forms.ComboBox)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.ComboBoxEdit)
                    {
                        if ((((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex < 0) && string.IsNullOrEmpty(((DevExpress.XtraEditors.ComboBoxEdit)c).Text))
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)c).Name))
                            {
                                error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.TextEdit)
                    {
                        if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)c).Name))
                            {
                                error = "Please enter " + required_list[((DevExpress.XtraEditors.TextEdit)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.TextBox)
                    {
                        if (string.IsNullOrEmpty(((System.Windows.Forms.TextBox)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.TextBox)c).Name))
                            {
                                error = "Please enter " + required_list[((System.Windows.Forms.TextBox)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.GroupBox)
                    {
                        foreach (Control sub_c in c.Controls)
                        {
                            if (sub_c is DevExpress.XtraEditors.TextEdit)
                            {
                                if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)sub_c).Text.Trim()))
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)sub_c).Name))
                                    {
                                        error = "Plaase enter " + required_list[((DevExpress.XtraEditors.TextEdit)sub_c).Name];
                                    }
                                }
                            }

                            if (sub_c is DevExpress.XtraEditors.ComboBoxEdit)
                            {
                                if ((((DevExpress.XtraEditors.ComboBoxEdit)sub_c).SelectedIndex < 0) && string.IsNullOrEmpty(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Text))
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name))
                                    {
                                        error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                DateTime date = DateTime.Parse(this.inputDate.EditValue.ToString());
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "INSERT INTO [231_UnitRR732] ([SerialNo], [Written], [Date], [Checked],"
                        + " [Wiring Daiagram], [PC software ver], [PC IP], [Host name], [RC400 ver], [RS485 ver],"
                        + " [PC Power], [PC Power Serial], [Upper RC420], [Upper RC420 Ver], [Upper RC420 Ex ver],"
                        + " [Upper RC420 No], [Upper RD026], [Upper run], [Upper stop], [Lower RC420], [Lower RC420 Ver],"
                        + " [Lower RC420 Ex ver], [Lower RC420 No], [Lower RD026], [Lower run], [Lower stop], [A RC420],"
                        + " [A RC420 Ver], [A RC420 Ex ver], [A RC420 No], [A RD026], [A run], [A stop], [Bar RC420],"
                        + " [Bar RC420 Ver], [Bar RC420 Ex ver], [Bar RC420 No], [Bar RD026], [Bar run], [Bar stop],"
                        + " [Z RC420], [Z RC420 Ver], [Z RC420 Ex ver], [Z RC420 No], [Z RD026], [Z run], [Z stop],"
                        + " [RC431], [RC431 ROM ver], [RC431 ver], [RC431 no], [Upper-OH], [Upper-OL], [Upper-PE],"
                        + " [Upper-OA], [Upper-OB], [Upper-OS], [Lower-OH], [Lower-OL], [Lower-PE], [Lower-OA], [Lower-OB],"
                        + " [Lower-OS], [A-OH], [A-OL], [A-PE], [A-OA], [A-OB], [A-OS], [B-OH], [B-OL], [B-PE], [B-OA],"
                        + " [B-OB], [B-OS], [Z-OH], [Z-OL], [Z-PE], [Z-OA], [Z-OB], [Z-OS], [Z-0S], [Inter-OH], [Inter-OL],"
                        + " [Inter-OA], [Inter-OB], [Inter-OS],[Remark])"
                        + " VALUES ('" + this.serialNo.Text.Trim() + "', '" + this.writtenBy.Text.Trim() + "',"
                        + " @date, '" + this.checkBy.Text.Trim() + "', '" + this.wiringDiagram.Text.Trim() + "',"
                        + " NULLIF('" + this.PC_Software_Ver.Text.Trim() + "', ''), NULLIF('" + this.PC_IP.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Host_Name.Text.Trim() + "', ''), NULLIF('" + this.RC400_Ver.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.RS485_Ver.Text.Trim() + "', ''), NULLIF('" + this.PC_Power.Text.Trim() + "', ''),"
                        + " '" + this.PC_Power_Serial.Text.Trim() + "',"
                        + " '" + this.Upper_RC420.Text.Trim() + "', '" + this.Upper_RC420Ver.Text.Trim() + "',"
                        + " NULLIF('" + this.Upper_RC420ExVer.Text.Trim() + "', ''),"
                        + " '" + this.Upper_RC420No.Text.Trim() + "', '" + this.Upper_RD026.Text.Trim() + "',"
                        + " '" + this.Upper_Run.Text.Trim() + "', '" + this.Upper_Stop.Text.Trim() + "',"
                        + " '" + this.Lower_RC420.Text.Trim() + "', '" + this.Lower_RC420Ver.Text.Trim() + "',"
                        + " NULLIF('" + this.Lower_RC420ExVer.Text.Trim() + "', ''),"
                        + " '" + this.Lower_RC420No.Text.Trim() + "', '" + this.Lower_RD026.Text.Trim() + "', "
                        + " '" + this.Lower_Run.Text.Trim() + "', '" + this.Lower_Stop.Text.Trim() + "',"
                        + " '" + this.A_RC420.Text.Trim() + "', '" + this.A_RC420Ver.Text.Trim() + "',"
                        + " NULLIF('" + this.A_RC420ExVer.Text.Trim() + "', ''),"
                        + " '" + this.A_RC420No.Text.Trim() + "', '" + this.A_RD026.Text.Trim() + "',"
                        + " '" + this.A_Run.Text.Trim() + "', '" + this.A_Stop.Text.Trim() + "',"
                        + " '" + this.Bar_RC420.Text.Trim() + "', '" + this.Bar_RC420Ver.Text.Trim() + "',"
                        + " NULLIF('" + this.Bar_RC420ExVer.Text.Trim() + "', ''),"
                        + " '" + this.Bar_RC420No.Text.Trim() + "', '" + this.Bar_RD026.Text.Trim() + "',"
                        + " '" + this.Bar_Run.Text.Trim() + "', '" + this.Bar_Stop.Text.Trim() + "',"
                        + " '" + this.Z_RC420.Text.Trim() + "', '" + this.Z_RC420Ver.Text.Trim() + "',"
                        + " NULLIF('" + this.Z_RC420ExVer.Text.Trim() + "', ''),"
                        + " '" + this.Z_RC420No.Text.Trim() + "', '" + this.Z_RD026.Text.Trim() + "',"
                        + " '" + this.Z_Run.Text.Trim() + "', '" + this.Z_Stop.Text.Trim() + "',"
                        + " '" + this.RC431.Text.Trim() + "', NULLIF('" + this.RC431_ROM_Ver.Text.Trim() + "', ''),"
                        + " '" + this.RC431Ver.Text.Trim() + "', '" + this.RC431No.Text.Trim() + "',"
                        + " NULLIF('" + this.Upper_OH.Text.Trim() + "', ''), NULLIF('" + this.Upper_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Upper_PE.Text.Trim() + "', ''), NULLIF('" + this.Upper_OA.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Upper_OB.Text.Trim() + "', ''), NULLIF('" + this.Upper_OS.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Lower_OH.Text.Trim() + "', ''), NULLIF('" + this.Lower_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Lower_PE.Text.Trim() + "', ''), NULLIF('" + this.Lower_OA.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Lower_OB.Text.Trim() + "', ''), NULLIF('" + this.Lower_OS.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A_OH.Text.Trim() + "', ''), NULLIF('" + this.A_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A_PE.Text.Trim() + "', ''), NULLIF('" + this.A_OA.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A_OB.Text.Trim() + "', ''), NULLIF('" + this.A_OS.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.B_OH.Text.Trim() + "', ''), NULLIF('" + this.B_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.B_PE.Text.Trim() + "', ''), NULLIF('" + this.B_OA.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.B_OB.Text.Trim() + "', ''), NULLIF('" + this.B_OS.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_OH.Text.Trim() + "', ''), NULLIF('" + this.Z_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_PE.Text.Trim() + "', ''), NULLIF('" + this.Z_OA.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_OB.Text.Trim() + "', ''), NULLIF('" + this.Z_OS.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_0S.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Inter_OH.Text.Trim() + "', ''), NULLIF('" + this.Inter_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Inter_OA.Text.Trim() + "', ''), NULLIF('" + this.Inter_OB.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Inter_OS.Text.Trim() + "', ''), '" + this.Remark.Text.Trim() + "')";
                    sql_cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date.Date;
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Database updated successfully!");
                    this.Dispose();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                }                
            }
            else
            {
                MessageBox.Show(error);
            }
        }
      
    }
}
